"""
pip config set global.index-url https://pypi.douban.com/simple
pip install mysqlclient
pip install pymysql
pip uninstall -y pymysql
pip list
pip freeze > requirements.txt
pip install -r requirements.txt
pip check
SQL注射攻击（SQL Injection）
"""
import MySQLdb

from utils import connect

no = int(input('编号：'))
name = input('名称：')
location = input('所在地：')

# 1. 创建连接对象
conn = connect(database='hrs')
try:
    # 2. 获取游标对象
    with conn.cursor() as cursor:
        # 3. 通过游标对象向数据库发出SQL语句获取执行结果
        affected_rows = cursor.execute(
            'insert into tb_dept values (%s, %s, %s)',
            (no, name, location)
        )
        if affected_rows == 1:
            print('新增部门成功')
    # 4. 提交事务
    conn.commit()
except MySQLdb.MySQLError as err:
    print(err)
    # 4. 回滚事务
    conn.rollback()
finally:
    # 5. 关闭数据库连接（释放资源）
    conn.close()
